import {
  DocumentObjectTreeNodeType,
  measureTypes,
  MenuItemDefinition,
  ObjectTreeMenuProps,
  ObjectTreeMenuUtils,
  sketchRootNodeTypes,
  Types,
  cmdInvoker,
} from '@amcax/base';
import { userEvents } from '@app-cad/module';
import { ContextMenuEvents } from './events';

export const MenuItemRename: MenuItemDefinition<ObjectTreeMenuProps> = {
  id: 'rename',
  children: '重命名',
  hidden: ({ props: { documentObject, selectedObjects } }) => {
    if (cmdInvoker.currentCmd || selectedObjects.length > 1) {
      return true;
    }
    const { type } = documentObject;
    if (type === Types.PARAMETER_SET) {
      const parentNode = (documentObject as DocumentObjectTreeNodeType).parent;

      if (parentNode != null && parentNode.parent == null) {
        return true;
      }
    }
    return (
      ObjectTreeMenuUtils.initialNodeTypes.includes(type) ||
      sketchRootNodeTypes.includes(type) ||
      measureTypes.includes(type) ||
      type === Types.PARAMETER_LINK
    );
  },

  onClick: ({ props: { documentObject } }) => {
    userEvents.emit(ContextMenuEvents.Rename, documentObject);
  },
};
